package utils

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"push/global"
)

type conf struct {
	//mysql-config
	MysqlHost string  `yaml:"mysql-host"`
	MysqlUser string  `yaml:"mysql-user"`
	MysqlPwd string   `yaml:"mysql-pwd"`
	MysqlTable string `yaml:"mysql-table"`
	//redis-config
	RedisHost string  `yaml:"redis-host"`
	RedisPwd string   `yaml:"redis-pwd"`
	RedisDB string    `yaml:"redis-db"`
}

//初始化加载配置文件
//@param  url:string
//@return *conf
func LoadYamlConfig(url string) *conf{
	var c conf
	conf := c.getConf(url)

	global.GLOBAL_MYSQL.Host = conf.MysqlHost
	global.GLOBAL_MYSQL.User = conf.MysqlUser
	global.GLOBAL_MYSQL.Pwd = conf.MysqlPwd
	global.GLOBAL_MYSQL.Table = conf.MysqlTable

	global.GLOBAL_REDIS.Host = conf.RedisHost
	global.GLOBAL_REDIS.Pwd = conf.RedisPwd
	global.GLOBAL_REDIS.DB = conf.RedisDB

	return conf
}

//读取文件进行IO处理
//@param url:string
//@return *conf
func (c *conf) getConf(url string) *conf {
	yamlFile, err := ioutil.ReadFile(url)
	if err != nil {
		fmt.Println(err.Error())
	}
	err = yaml.Unmarshal(yamlFile, c)
	if err != nil {
		fmt.Println(err.Error())
	}
	return c
}